package de.jmda.util.gui.swing;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;

import de.jmda.gui.swing.mvc.AbstractController;
import de.jmda.gui.swing.mvc.ControllerContextProvider;

/**                                                                                    
 * This class was initially generated by {@link de.jmda.gui.swing.mvc.ControllerSupportEnabledGenerator}.    
 * <p>                                                                                 
 * <code>ControllerSupportEnabledGenerator</code> will not regenerate this class,
 * hence modifications will not get lost due to subsequent generation by <code>ControllerSupportEnabledGenerator</code>.                                         
 */
class JPanelFavouritesController
		extends AbstractController<JPanelFavouritesModel>
{
	private JPanelFavouritesControllerContext context;
	
	private JPanelFavouritesModel model;
	
	private List<File> favourites = new LinkedList<File>();
	
	private File currentDirectory;
	
	private List<ChangeDirectoryListener> changeDirectoryListeners =
			new LinkedList<ChangeDirectoryListener>();

	/**                                                                                   
	 * Constructor                                                                        
	 */
	JPanelFavouritesController(ControllerContextProvider controllerContextProvider)
	{
		super(controllerContextProvider);
		refreshControllerContext();
	}

  /**
   * @see de.jmda.gui.swing.mvc.AbstractController#setModel(de.jmda.gui.swing.mvc.ModelBase)
   */
  @Override
  public void setModel(JPanelFavouritesModel model)
  {
		this.model = model;

		// initialise jList favourites
		JList jList = context.getJListFavourites();

		if (jList == null)
		{
			// jList is not initialised yet, leave
			return;
		}

		favourites = this.model.getFavourites();

//		if (favourites.isEmpty())
//		{
//			// mock values for favourites of component
//			favourites.add(new File("1"));
//			favourites.add(new File("2"));
//			favourites.add(new File("3"));
//		}

		// populate context.jListFavourites
		DefaultListModel defaultListModel = new DefaultListModel();

		for (File file : favourites)
		{
			defaultListModel.addElement(file);
		}

		jList.setModel(defaultListModel);
		
		if (defaultListModel.getSize() == 0)
		{
			context.getJButtonUp().setEnabled(false);
			context.getJButtonDown().setEnabled(false);
			context.getJButtonRemove().setEnabled(false);
			context.getJButtonChangeDirectory().setEnabled(false);
		}

		jList.setSelectedIndex(0);
  }

	@Override
	public void refreshControllerContext()
	{
		context =
		    (JPanelFavouritesControllerContext)
		    		controllerContextProvider.getControllerContext();
	}
	
	public boolean addChangeDirectoryListener(
			ChangeDirectoryListener changeDirectoryListener)
	{
		return changeDirectoryListeners.add(changeDirectoryListener);
	}
	
	public boolean removeChangeDirectoryListener(
			ChangeDirectoryListener changeDirectoryListener)
	{
		return changeDirectoryListeners.remove(changeDirectoryListener);
	}

	public void setCurrentDirectory(File currentDirectory)
  {
		this.currentDirectory = currentDirectory;
  }

	public void listSelectionValueChanged(ListSelectionEvent evt)
	{
		int selectedIndex = context.getJListFavourites().getSelectedIndex();
		
		// enable / disable jButtonUp
		if (selectedIndex < 1)
		{
			context.getJButtonUp().setEnabled(false);
		}
		else
		{
			context.getJButtonUp().setEnabled(true);
		}
		
		int listSize = context.getJListFavourites().getModel().getSize();
		
		// enable / disable jButtonDown
		if (listSize > 1)
		{
			if (selectedIndex < (listSize - 1))
			{
				context.getJButtonDown().setEnabled(true);
			}
			else
			{
				context.getJButtonDown().setEnabled(false);
			}
		}
		else
		{
			context.getJButtonDown().setEnabled(false);
		}
		
		// enable / disable jButtonRemove
		if (selectedIndex > -1)
		{
			context.getJButtonRemove().setEnabled(true);
		}
		else
		{
			context.getJButtonRemove().setEnabled(false);
		}
		
		// enable / disable jButtonChangeDirectory
		if (selectedIndex > -1)
		{
			context.getJButtonChangeDirectory().setEnabled(true);
		}
		else
		{
			context.getJButtonChangeDirectory().setEnabled(false);
		}
	}

	public void upActionPerformed()
  {
		JList jList = context.getJListFavourites();
		
		ListModel listModelOld = jList.getModel();
		
		int selectedIndex = jList.getSelectedIndex();
		
		Object selectedObject = listModelOld.getElementAt(selectedIndex);
		Object selectedObjectPredecessor = null;
		
		DefaultListModel listModelNew = new DefaultListModel();
		
		for (int i = 0; i < listModelOld.getSize(); i++)
		{
			if (i == (selectedIndex - 1))
			{
				selectedObjectPredecessor = listModelOld.getElementAt(i);
			}
			else if (i == selectedIndex)
			{
				listModelNew.addElement(selectedObject);
				listModelNew.addElement(selectedObjectPredecessor);
			}
			else
			{
				listModelNew.addElement(listModelOld.getElementAt(i));
			}
		}
		
		jList.setModel(listModelNew);
		jList.setSelectedIndex(selectedIndex - 1);
  }

	public void downActionPerformed()
  {
		JList jList = context.getJListFavourites();
		
		ListModel listModelOld = jList.getModel();
		
		int selectedIndex = jList.getSelectedIndex();
		
		Object selectedObject = listModelOld.getElementAt(selectedIndex);
		
		DefaultListModel listModelNew = new DefaultListModel();
		
		for (int i = 0; i < listModelOld.getSize(); i++)
		{
			if (i == selectedIndex)
			{
				// do nothing, selectedObject is already known and will be added to new
				// list favourites later
			}
			else if (i == (selectedIndex + 1))
			{
				// add object at i first, then add selected object
				listModelNew.addElement(listModelOld.getElementAt(i));
				listModelNew.addElement(selectedObject);
			}
			else
			{
				listModelNew.addElement(listModelOld.getElementAt(i));
			}
		}
		
		jList.setModel(listModelNew);
		jList.setSelectedIndex(selectedIndex + 1);
  }

	public void removeActionPerformed()
  {
		JList jList = context.getJListFavourites();
		
		int selectedIndex = jList.getSelectedIndex();

		// update model
		favourites.remove(selectedIndex);
		// update view
		((DefaultListModel) jList.getModel()).remove(selectedIndex);
		
		jList.setSelectedIndex(selectedIndex);
  }

	public void insertActionPerformed(ActionEvent evt)
  {
		JList jList = context.getJListFavourites();

		// update model
		favourites.add(currentDirectory);
		// update view
		((DefaultListModel) jList.getModel()).add(0, currentDirectory);

		jList.setSelectedIndex(0);
  }

	/**
	 * @param evt
	 */
	public void changeDirectoryActionPerformed(ActionEvent evt)
  {
		JList jList = context.getJListFavourites();
		
		ListModel listModel = jList.getModel();
		
		int selectedIndex = jList.getSelectedIndex();
		
		File selectedFavourite = (File) listModel.getElementAt(selectedIndex);
		
		for (ChangeDirectoryListener changeDirectoryListener :
		     changeDirectoryListeners)
		{
			changeDirectoryListener.changeDirectory(selectedFavourite);
		}
  }

	public void propertyChange(PropertyChangeEvent evt)
  {
		if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName()))
		{
			setCurrentDirectory((File) evt.getNewValue());
		}
		else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName()))
		{
			Object newValue = evt.getNewValue();
			
			if (newValue != null)
			{
				File file = (File) evt.getNewValue();

				if (file.isDirectory())
				{
					setCurrentDirectory(file);
				}
			}
		}
  }
}